
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;

public class JavaTest {
	
	public void test1(){
		String url = "http://bj.58.com/fengtai/ywjiajiaogeren/pve_9412_661277_pve_9405_661268/";
		System.out.println(url.length());
	}
	
	public String formatHref(String href){
		String result = "";
		if(StringUtils.isNotBlank(href)){
			String url = href.split("\\?")[0];
			String patternString = "http://[^/]*|https://[^/]*";
			Pattern pattern = Pattern.compile(patternString); 
		    Matcher matcher = pattern.matcher(url); 
		    
		    if(matcher.find()){
		    	String urltmp = url.replace(matcher.group(), "");
		    	if(StringUtils.isBlank(urltmp)){ // 去掉域名后 空  用去参的href代替
					result = url;
		    	}else{	// 获取相对路径				
					result = urltmp;
		    	}
		    }else{ // href 不以http://开头
		    	result = url;
		    }
			
		}else{ // href 空
			return "";
		}		
		return result;
	}
	
	public void test2(){
		String url = "https://bj.58.com/fengtai/ywjiajiaogeren/pve_9412_661277_pve_9405_661268/";
		
		System.out.println(formatHref(url));
		
		
	}

	public static void main(String[] args) throws UnsupportedEncodingException {
		String urString = "/jianzhi/jh_%2525u517C%2525u804C%2525u62DB%2525u8058/";
		System.out.println(URLDecoder.decode(urString,"UTF-8"));
		
		String url = "<a href=\"http://bj.58.partner.mayi.com/?ca_s=tg_58&ca_n=bj&from=sy_fangchan_minsuyuding\" rel=\"nofollow\"  onclick=\"clickLog('from=sy_fangchan_minsuyuding');\">民宿预订</a>";
		String 	PATTERN = "http://(bj.58).*\\?.*?(ca_n=.*)?&fr";
		Pattern pattern = Pattern.compile(PATTERN); 
	    Matcher matcher = pattern.matcher(url); 
	    
	    if(matcher.find()){
	    	System.out.println(matcher.group());
	    	System.out.println(matcher.group(0));
	    	System.out.println(matcher.group(1));
	    	System.out.println(matcher.group(2));
	    }else{
	    	System.out.println("not found!");
	    }
	}
}
